<?php
class LeftNavWidget  extends CWidget{
    private $showSum  = 20;
	//输出字符串
	private $out = '';
    
    private $controller= '';
    
    private $action= '';
    
    public $category='';

	public function init(){
        $this->controller = Yii::app()->controller->id;  
        $this->action = Yii::app()->controller->action->id; 
	}

    public function run(){
		$this->configSearchItem();
        $this->configLatestBlogItem();
        $this->configRankBlogItem();
        $this->configBlogCategoryItem();
        $this->configFriendItem();

		echo $this->out;
	}
    

	/**
	 * 配置搜索框
	 *
	 * @return void
	 * @author Durban
	 */
    public function configSearchItem(){
		$search_url = Yii::app()->createUrl('site/search');
        $this->out .= <<<SEARCH
			<section class='well'>
				<div class="form-group has-success">
					<form class="navbar-form pull-left" method='GET' action='{$search_url}'>
			            <input type="text" name='key' style='margin-bottom:10px' class="form-control span12" value="">
						<button type="submit" class="btn btn-sm btn-primary ">搜索</button>
					</form>
		        </div>
			</section>
SEARCH;
    }    

	
	/**
	 * 配置文章的目录类别
	 *
	 * @return void
	 * @author Durban
	 **/
    public function configBlogCategoryItem(){
        $criteria=new CDbCriteria();
        $criteria->condition = 'category=:category AND is_lock = 0 AND is_delete = 0';
        $criteria->params = array(':category'=>'blog');
        $criteria->order = 'name ASC';
        $type = Type::model()->findAll($criteria);
        $categoryItem = '';
        foreach($type as $k=>$v){
            $j = $k+1;
            $tagDisable = $j==1 ? 'tagdisabled' : 'tag';
            $tagDisable = 'tag';
            $tagUrl = Yii::app()->createUrl('site/category',array('category'=>urlencode($v->name)));
            $tagName = $v->name;
            $sum = $this->getCategorySum($v->id);
            $categoryItem .= <<<ITEM
            <li class="category-list">
                <a title='{$tagName}' href="{$tagUrl}">
                    <span class="fui-chat"></span> {$tagName}<span style='float:right;padding:right:10px'><span class="label label-inverse">{$sum}</span></span>
                </a>
            </li>
ITEM;
        }

        $category = <<<CATEGORY
			<section class='well'>
				<h5>
					文章分类
					<img style='height: 30px;width: 40px;float: right;' title='文章分类' alt='文章分类' src='/images/illustrations/book.png'>
				</h5>
				<div class='divider'></div>
				<ul class='item'>
					{$categoryItem}
				</ul>
			</section>
CATEGORY;
        $this->out .= $category;                
    }

    
    /**
     * 获取文章类型的文章数量
     * @param type $category_id
     * @return type
     */
    public function getCategorySum($category_id){
        $criteria = new CDbCriteria();
        $criteria->condition = 'type_id=:type_id';
        $criteria->params = array(':type_id'=>$category_id);
        $count = Blog::model()->count($criteria);
        return $count;
    }
    
	/**
	 * 配置最近文章列表
	 *
	 * @return void
	 * @author Durban
	 **/
    public function configLatestBlogItem(){
        $blogItem = '';
        
        $criteria=new CDbCriteria();
        $criteria->condition = 'is_lock = 0 AND is_delete = 0';
        $criteria->limit = $this->showSum;
        $criteria->offset = 0;
        $criteria->order = 'create_date DESC';
        $blog = Blog::model()->findAll($criteria);
        foreach($blog as $k=>$v){
            $blogUrl = Yii::app()->createUrl('site/view',array('id'=>$v->id));
            $blogName = $v->title;
            $blogShortName = HelperString::sysSubStr($v->title, 30, true);
            $blogItem .= <<<BLOG
                 <li class="item-list">
                    <a title='{$blogName}' href="{$blogUrl}">
                        <span class="fui-video"></span> {$blogShortName}
                    </a>
                </li>
BLOG;
        }
        
        $latest = <<<LATEST
			<section class='well'>
				<h5>
					最新文章
					<img style='height: 30px;width: 40px;float: right;' title='最新文章' alt='最新文章' src='/images/illustrations/clipboard.png'>
				</h5>
				<div class='divider'></div>
				<ul class='item'>
					{$blogItem}
				</ul>
			</section>
LATEST;
        $this->out .= $latest;    
    }
    
	/**
	 * 配置文章排名列表
	 *
	 * @return void
	 * @author Durban
	 **/
    public function configRankBlogItem(){
        $blogItem = '';
        
        $criteria=new CDbCriteria();
        $criteria->condition = "is_lock = 0 AND is_delete = 0";
        $criteria->limit = $this->showSum;
        $criteria->offset = 0;
        $criteria->order = 'read_sum DESC,create_date DESC';
        $blog = Blog::model()->findAll($criteria);
        foreach($blog as $k=>$v){
            $blogUrl = Yii::app()->createUrl('site/view',array('id'=>$v->id));
            $blogName = $v->title;
            $blogShortName = HelperString::sysSubStr($v->title, 30, true);
            $blogItem .= <<<BLOG
                 <li class="item-list">
                    <a title='{$blogName}' href="{$blogUrl}">
                        <span class="fui-video"></span> {$blogShortName}
                    </a>
                </li>
BLOG;
        }
        
        $rank = <<<RANK
			<section class='well'>
				<h5>
					阅读排行
					<img style='height: 30px;width: 40px;float: right;' title='文章分类' alt='文章分类' src='/images/illustrations/retina.png'>
				</h5>
				<div class='divider'></div>
				<ul class='item'>
					{$blogItem}
				</ul>
			</section>
RANK;
        $this->out .= $rank;
    }

	/**
	 * 配置友情链接的网址列表
	 *
	 * @return void
	 * @author Durban
	 **/
    public function configFriendItem(){
        $linkItem = '';
        
        $studyId = $this->getFriendId();
        
        if(!empty($studyId)){
            $criteria=new CDbCriteria();
            $criteria->condition = 'category_id=:category_id AND is_lock = 0 AND is_delete = 0';
            $criteria->params = array(':category_id'=>$studyId);
            $criteria->order = 'create_date DESC';
            $link = Link::model()->findAll($criteria);
        }else{
            $link = array();
        }
        
        
        foreach($link as $k=>$v){
            $linkUrl = $v->url_address;
            $linkName = $v->title;
            $linkShortName = HelperString::sysSubStr($v->title, 30, true);
            $linkItem .= <<<BLOG
                 <li class="item-list">
                    <a target='_blank' title='{$linkName}' href="{$linkUrl}">
                        <span class="fui-eye"></span> {$linkShortName}
                    </a>
                </li>
BLOG;
        }
        
        $learn = <<<LEARN
			<section class='well'>
				<h5>
					友情链接
					<img style='height: 30px;width: 40px;float: right;' title='文章分类' alt='文章分类' src='/images/illustrations/gift.png'>
				</h5>
				<div class='divider'></div>
				<ul class='item'>
					{$linkItem}
				</ul>
			</section>
LEARN;
        $this->out .= $learn;
    }
    
    /**
     * 获取学习网址
     * @return string
     */
    public function getFriendId(){
        $keyWord = '友情网址';
        
        $criteria=new CDbCriteria();
        $criteria->condition = "name=:name AND category = 'link'";
        $criteria->params = array(':name'=>$keyWord);
        $typeObj = Type::model()->findAll($criteria);
        if(!empty($typeObj)){
            foreach($typeObj as $k=>$v){
                if(!empty($v->id)){
                    return $v->id;
                }
            }
        }
        return '';
    }

    
    public function generateLeftTaobaoAd(){
        
        $this->out .= <<<AD
                <div class='' style='padding:6px'>
                <script type="text/javascript">
                    document.write('<a style="display:none!important" id="tanx-a-mm_36626661_3506017_13400646"></a>');
                    tanx_s = document.createElement("script");
                    tanx_s.type = "text/javascript";
                    tanx_s.charset = "gbk";
                    tanx_s.id = "tanx-s-mm_36626661_3506017_13400646";
                    tanx_s.async = true;
                    tanx_s.src = "http://p.tanx.com/ex?i=mm_36626661_3506017_13400646";
                    tanx_h = document.getElementsByTagName("head")[0];
                    if(tanx_h)tanx_h.insertBefore(tanx_s,tanx_h.firstChild);
                </script>
                </div>
AD;
	}

	public function generateLeltTaobaoRecharge(){
		$this->out .= <<<RECHARGE
			<div class='' style='padding:6px'>
			<iframe name="alimamaifrm" frameborder="0" marginheight="0" marginwidth="0" border="0" scrolling="no" width="250" height="200" src="http://www.taobao.com/go/app/tbk_app/chongzhi_210_200.php?pid=mm_36626661_3506017_11481833&page=chongzhi_210_200.php&size_w=210&size_h=200&stru_phone=1&stru_game=1&stru_travel=1&size_cat=std" ></iframe>	
			</div>	
RECHARGE;
	}

	public function generateChineseAd(){
		$this->out .= <<<CHINESE
		<div clsss='' style='padding:6px'>
			<iframe src='http://www.chinesean.com/affiliate/box.do?bid=15930' width='250' height='600' frameBorder='0' border='0' scrolling='no'></iframe>
		</div>
CHINESE;
	}

	/**
	 * google首页的广告位
	 */
	public function generateGoogleAdCaptionLeft(){
		if(!($this->controller == 'site' && $this->action=='view')){
			$this->out .= <<<CAPTIONLEFT
				<div class='' style='padding:6px'>
					<script type="text/javascript"><!--
					google_ad_client = "ca-pub-2785609193103098";
					/* 首页左侧导航广告位 */
					google_ad_slot = "5255314361";
					google_ad_width = 250;
					google_ad_height = 250;
					//-->
					</script>
					<script type="text/javascript"
					src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
					</script>
				</div>
CAPTIONLEFT;
		}
	}
    
    public function generateAmazon(){
        $this->out .= <<<Amazon
				<div class='' style='padding:6px'>
					<iframe src="http://rcm-cn.amazon-adsystem.com/e/cm?t=gowhich-23&o=28&p=12&l=ur1&category=books&banner=1K3RQR56FPEJFEZ8R9R2&f=ifr" width="250" height="250" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"></iframe>
				</div>
Amazon;
    }
}


